/*********************************************************
          File Name:19.1.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 17 May 2016 08:27:28 PM CST
**********************************************************/

#include <iostream>
#include <cstdlib>

void *operator new(std::size_t size)
{
  std::cout << "new\n"; // print twice
  if(void *res = malloc(size))
  {
    printf("%d -> %p\n", __LINE__, res);
    return res;
  }
  throw std::bad_alloc();
}

void operator delete(void *mem) noexcept
{
  std::cout << "delete\n";  // print twice
  printf("%d -> %p\n", __LINE__, mem);
  free(mem);
}

class Test
{
  public:
    Test(int i)
    {
      buf = new int[i];
      printf("%d -> %p\n", __LINE__, buf);
    }
    ~Test()
    {
      std::cout << "delete buf\n";
      delete [] buf;
      buf = nullptr;
    }
  private:
    int *buf;
};

int main()
{
  Test *t = new Test(10);
  t->~Test();
  printf("%d -> %p\n", __LINE__, t);
  delete t;
}
